import Vue from 'vue'
import Vuex from 'vuex'
import Types from './types'

Vue.use(Vuex)

const state = {
  loginStatus: false,
  userInfo: {}
}

const getters = {
  loginStatus: state => state.loginStatus,
  userInfo: state => state.userInfo
}

// 同步操作
const mutations = {
  [Types.SET_LOGIN_STATUS]:(state, loginStatus) => {
    if (loginStatus) state.loginStatus = loginStatus
    else state.loginStatus = false
  },
  [Types.SET_USER_INFO]:(state, userInfo) => {
    if(userInfo) state.userInfo = userInfo
    else state.userInfo = {}
  }
}

// 可以异步操作,操作mutations
const actions = {
  // example: name: ( {commit}, params ) => {}
  setLoginStatus: ({ commit }, loginStatus ) => {
    commit(Types.SET_LOGIN_STATUS, loginStatus)
  },
  setUserInfo: ({ commit }, userInfo) => {
    commit(Types.SET_USER_INFO, userInfo)
  }
}

const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
})

export default store;